import Vue from 'vue'
import VueRouter from 'vue-router'

import Main from '../pages/main.vue'
import Login from '../pages/login.vue'

const files = require.context('../pages', true, /[a-zA-Z]\.vue$/);

let routes = files.keys().map(item => {
    let path = item.slice(1, -4)
    let name = item.slice(1, -4)
    let componentName = files(item).default.componentTitle

    return {
        path: path,
        name: name,
        component: files(item).default,
        mate: {
            title: componentName
        }
    }
})

Vue.use(VueRouter)

const router = new VueRouter({
    routes: [
        {
            path: '/',
            redirect: '/home'
        },
        {
            path: '/main',
            name: '/main',
            component: Main,
            children: routes
        },
        {
            path: '/login',
            name: '/login',
            component: Login
        }
    ]
})

export default router